

*Set sub-directory paths--same for all users:

global unacast  "${machine}/data/clean/unacast"
global stay_at_home "${machine}/data/clean/stay_at_home"
global womply "${machine}/data/clean/womply"
global nyt_cases "${machine}/data/clean/nyt_cases"
global census "${machine}/data/clean/census"
global trump "${machine}/data/clean/trump"
global acs "${machine}/data/clean/acs_2018"
global usda "${machine}/data/clean/usda_ers"
global figures "${machine}/figures/calendar_time"

clear

use "${stay_at_home}/stay_at_home_AM.dta"


**drop dates with no social distance data
merge 1:1 county_fips date using "${unacast}/social_distance_county_04_29.dta"
drop _merge

merge 1:1 county_fips date using "${nyt_cases}/nyt_cases_04_17.dta"
drop if date<= 21968 
tab _merge
drop _merge

**drop dates after April 17 (10 days after last order on April 7)
drop if date> 22022

foreach x in tot_pop effective_date {
bysort county_fips: egen `x'_t=mean(`x')
drop `x'
rename `x'_t `x'
}
format effective_date %td


** 88 counties don't show up in the social distance data, but these counties have very small pop

bysort county_fips: egen ever_stayathome=max(stay_at_home)

gen eff_date=1 if effective_date>=21991 & effective_date<=21995 & effective_date!=.
replace eff_date=2 if effective_date>=21996 & effective_date<= 22000 & effective_date!=.
replace eff_date=3 if effective_date>= 22001 & effective_date<= 22005 & effective_date!=.
replace eff_date=4 if effective_date>= 22006 & effective_date<= 22012 & effective_date!=.
replace eff_date=5 if effective_date==.

gen weekend=1 if weekday==1 | weekday==7

drop state_fips
gen temp=county_fips
format temp %05.0f
tostring temp,  replace usedisplayformat
gen state_fips=substr(temp ,1,2)
destring state_fips, replace
drop temp


preserve
use "${womply}/womply_daily_revenue", clear
keep if year(date)==2020
keep if !missing(county_fips)

tab category, m

gen clean_cat="other"
replace clean_cat="food" if inlist(category,"bars and lounges","food and beverage shops","quick serve food and beverage businesses","restaurants")
tab category clean_cat, m

keep date county_fips revenue clean_cat intra_covid_active_merchants intra_covid_inactive_merchants
rename revenue revenue_
rename intra_covid_active_merchants active_
rename intra_covid_inactive_merchants inactive_

collapse (sum) revenue_ active_ inactive_, by(date county clean_cat)

reshape wide revenue_ active_ inactive_, i(date county_fips) j(clean_cat) string

*Make 'all revenue' category
egen revenue_all = rowtotal(revenue_*)
egen inactive_all = rowtotal(inactive_*)
egen active_all = rowtotal(active_*)




*Missing observations in a date*county*category are instances with zero
*	revenue for that category of business.

foreach var of varlist revenue* {
	replace `var'=0 if missing(`var')
	
	gen log`var'=log(`var'+1)
}


**drop dates after April 17 (10 days after last order on April 7)
drop if date> 22022

tempfile womply
save "`womply'"
restore

merge 1:1 date county_fips using "`womply'", nogen


*Merge on USDA county-level characteristics

merge m:1 county_fips using "${usda}/county_information.dta", nogen
reg tot_pop pop_estimate_2018

*Create revenue per-capita estimates from the Womply panel

foreach v in food all other {
display "`var'"
	gen perest_revenue_`v'=revenue_`v'/(inactive_`v'+active_`v')
}

rename perest_revenue_food pe_r_food
rename perest_revenue_other pe_r_other
rename perest_revenue_all pe_r_all


rename daily_distance_diff distance
rename daily_visitation_diff visit
 
 
*Merge on Trump vote share
preserve
import delimited using "${trump}/2016_US_County_Level_Presidential_Results.csv", clear

rename combined_fips county_fips
format county_fips %05.0f
gen gop_win_pct = per_gop-per_dem
keep county_fips gop_win_pct

sum gop_win_pct, d

tempfile trump
save "`trump'"
restore

merge m:1 county_fips using "`trump'", nogen

gen split_trump=gop_win_pct>0 & !missing(gop_win_pct)
gen split_dem=gop_win_pct<=0 & !missing(gop_win_pct)

*Merge on USDA county-level characteristics

merge m:1 county_fips using "${usda}/county_information.dta", nogen
reg tot_pop pop_estimate_2018



*Create low- and high-income bins using top and bottom quartile
*	median household income in 2018

preserve
keep county_fips median_household_income_2018
duplicates drop
sum median_household_income_2018, d

xtile quantile_inc=median_household_income_2018, n(4)
tab quantile_inc, m

gen inc_low=inrange(quantile_inc,1,1)
replace inc_low=. if missing(quantile_inc)

gen inc_high=inrange(quantile_inc,4,4)
replace inc_high=. if missing(quantile_inc)

gen inc_all=1

keep county_fips inc_*

tempfile qinc
save "`qinc'"

restore

merge m:1 county_fips using "`qinc'", nogen
 
 gen split_early=1 if effective_date>=21991 & effective_date<=22000 & effective_date!=.
gen split_late=1 if effective_date>= 22001 & effective_date<= 22012
drop if state_fips==72

rename encounters_rate encounter

***histogram of effective date
graph twoway (hist effective_date , xlab(21991  21999 22007) scale(*1.4) frac legend(off) xti("") discrete scheme(s1color) color(navy%60) )
graph export "${figures}/hist_total.pdf", replace 

graph twoway (hist effective_date if split_trump==1 , xlab(21991  21999 22007) scale(*1.4)  xti("")  frac discrete scheme(s1color) color(cranberry%60))  ///
 (hist effective_date if split_dem==1 , legend(order(1 "Republican (2016)" 2 "Democrat (2016)")  col(3) region(lcolor(none)) ) frac discrete color(none) lcolor(blue) )
graph export "${figures}/hist_trumpsplit.pdf", replace 

graph twoway (hist effective_date if inc_low==1 , xlab(21991  21999 22007) scale(*1.4) xti("") frac discrete scheme(s1color) color(lavender%60) ) ///
 (hist effective_date if inc_high==1 , legend(order(1 "Low income" 2 "High income")  col(3) region(lcolor(none)) ) frac discrete color(none) lcolor(pink) )
graph export "${figures}/hist_incsplit.pdf", replace
 
graph twoway (hist effective_date if split_early==1 , xlab(21991  21999 22007) scale(*1.4) xti("") frac discrete scheme(s1color) color(midgreen%60) ) ///
 (hist effective_date if split_late==1 , legend(order(1 "Early orders" 2 "Late orders")  col(3) region(lcolor(none)) ) frac discrete color(none) lcolor(gold) )
graph export "${figures}/hist_timingsplit.pdf", replace
  
  
  
 
local visit_b "-70"
local distance_b "-60"

foreach v in  distance visit  {
***plot social distance vote share
preserve 
drop if date> 22022
tab date if distance!=.

collapse (max) weekend (mean) distance visit (rawsum) tot_pop [aweight=tot_pop], by(date split_trump)
format  tot_pop  %12.0g

gen weekendl=weekend*``v'_b'
gen weekendh=weekend*10
bysort date: sum weekendl

gen line=0
sort date split_trump
graph twoway ///
(rarea weekendh weekendl date , scale(*1.2) yaxis(1) sort xlab( 21977 21991 22005 22019) ttext( 5 21991 "First order", place(e) ) xline(21991,lcolor(black)) color(gs14) cmissing(n) ylabel(10(20)``v'_b', grid))   ///
(line line date, color(gs4)) ///
(line `v' date if split_trump==0, yti("Percent change" ) lcolor(blue) scheme(s1color)  ///
legend(order(3 "Democrat (2016)" 4 "Republican (2016)" ) col(2) region(lcolor(none)) ) xti("")) ///
(line `v' date if split_trump==1, lcolor("228 95 71"))

graph export "${figures}/`v'_trumpsplit_cal.pdf", replace
restore
}

foreach v in  distance visit  {
***plot social distance by timing of stay at home order
preserve 
drop if date> 22022
collapse (max) weekend (mean) distance visit (rawsum) tot_pop [aweight=tot_pop], by(date eff_date)
format  tot_pop  %12.0g

gen weekendl=weekend*``v'_b'
gen weekendh=weekend*10
bysort date: sum weekendl

gen line=0
sort date eff_date
graph twoway ///
(rarea weekendh weekendl date , scale(*1.3) yaxis(1) sort ttext( 5 21991 "First order", place(e) ) xlab(21977 21991 22005 22019) xline(21991, lcolor(black))  color(gs14) cmissing(n) ylabel(10(20)``v'_b', grid))   ///
(line line date, color(gs4)) ///
(line `v' date if eff_date==1, yti("Percent change" ) lcolor("89 13 17") scheme(s1color)  ///
legend(order(3 "Early: 3/17-3/21" 4 "Mid: 3/22-3/26" 5 "Later: 3/27-3/31" 6 "Late: 4/01-4/07 "  7 "No order") row(3) region(lcolor(none)) ) xti("")) ///
(line `v' date if eff_date==2, lcolor("187 47 41")) ///
(line `v' date if eff_date==3, lcolor("228 95 71")) ///
(line `v' date if eff_date==4,  lcolor("238  151 121"))  ///
(line `v' date if eff_date==5,  lcolor("243 190 165"))  

graph export "${figures}/`v'_caltime.pdf", replace
restore

}

local encounter_b "100"

foreach v in  encounter  {
***plot social distance---encounter rate---by timing of stay at home order
preserve 
drop if date> 22022
collapse (max) weekend (mean) encounter (rawsum) tot_pop [aweight=tot_pop], by(date eff_date)
format  tot_pop  %12.0g

gen weekendl=weekend*1
gen weekendh=weekend*``v'_b'
bysort date: sum weekendl

gen line=0
sort date eff_date
graph twoway ///
(rarea weekendh weekendl date , scale(*1.3) yaxis(1) sort ttext( 80 21991 "First order", place(e) ) xlab(21977 21991 22005 22019) xline(21991,lcolor(black))  color(gs14) cmissing(n) ylabel(``v'_b'(20)0, grid))   ///
(line line date, color(gs4)) ///
(line `v' date if eff_date==1, yti("Encounter rate" ) lcolor("89 13 17") scheme(s1color)  ///
legend(order(3 "Early: 3/17-3/21" 4 "Mid: 3/22-3/26" 5 "Later: 3/27-3/31" 6 "Late: 4/01-4/07 "  7 "No order") row(3) region(lcolor(none)) ) xti("")) ///
(line `v' date if eff_date==2, lcolor("187 47 41")) ///
(line `v' date if eff_date==3, lcolor("228 95 71")) ///
(line `v' date if eff_date==4,  lcolor("238  151 121"))  ///
(line `v' date if eff_date==5,  lcolor("243 190 165"))  

graph export "${figures}/`v'_caltime.pdf", replace
restore
}

local pe_r_food_b "14000"
local pe_r_other_b "7500"
local pe_r_food_b_c "3500"
local pe_r_other_b_c "2500"
foreach v in  pe_r_food pe_r_other  {
***plot social distance by timing of stay at home order
preserve 
tab date if pe_r_food!=.
gen t=date
tab t if pe_r_food!=.

drop if date> 22022
collapse (max) weekend (mean)pe_r_food pe_r_other (rawsum) tot_pop [aweight=tot_pop], by(date eff_date)
format  tot_pop  %12.0g

local `v'_lab=``v'_b'*.8

gen weekendl=weekend*0.01
gen weekendh=weekend*``v'_b'
bysort date: sum weekendl

gen line=0
sort date eff_date
graph twoway ///
(rarea weekendh weekendl date , scale(*1.3) yaxis(1) ttext( ``v'_lab' 21991 "First order", place(e) ) sort xline(21991,lcolor(black))  xlab(21977 21991 22005 22019) color(gs14) cmissing(n) ylabel(0(``v'_b_c')``v'_b', grid))   ///
(line line date, color(gs4)) ///
(line `v' date if eff_date==1, yti("Revenue per establishment ($)" "") lcolor("89 13 17") scheme(s1color)  ///
legend(order(3 "Early: 3/17-3/21" 4 "Mid: 3/22-3/26" 5 "Later: 3/27-3/31" 6 "Late: 4/01-4/07 "  7 "No order") row(3) region(lcolor(none)) ) xti("")) ///
(line `v' date if eff_date==2, lcolor("187 47 41")) ///
(line `v' date if eff_date==3, lcolor("228 95 71")) ///
(line `v' date if eff_date==4,  lcolor("238  151 121"))  ///
(line `v' date if eff_date==5,  lcolor("243 190 165"))  

graph export "${figures}/`v'_caltime.pdf", replace
restore
}

replace cases=cases/1000

local cases_b "5"
local deaths_b "220"
local cases_b_c "1"
local deaths_b_c "55"
local cases_daily_incr_b "300"
local cases_daily_incr_b_c "100"
local deaths_daily_incr_b "15"
local deaths_daily_incr_b_c "5"
foreach v in   deaths  cases_daily_incr deaths_daily_incr cases {
***plot social distance by timing of stay at home order
preserve 
drop if date> 22022
tab date
collapse (max) weekend (mean) cases deaths cases_daily_incr deaths_daily_incr (rawsum) tot_pop [aweight=tot_pop], by(date eff_date)
format  tot_pop  %12.0g

local `v'_lab=``v'_b'*.8

gen weekendl=weekend*0.01
gen weekendh=weekend*``v'_b'
bysort date: sum weekendl

gen line=0
sort date eff_date
graph twoway ///
(rarea weekendh weekendl date , scale(*1.3) yaxis(1)  ttext( ``v'_lab' 21991 "First order", place(e) ) sort xline(21991,lcolor(black)) xlab(21977 21991 22005 22019)  color(gs14) cmissing(n) ylabel(0(``v'_b_c')``v'_b', grid))   ///
(line line date, color(gs4)) ///
(line `v' date if eff_date==1, yti("`v'" "") lcolor("89 13 17") scheme(s1color)  ///
legend(order(3 "Early: 3/17-3/21" 4 "Mid: 3/22-3/26" 5 "Later: 3/27-3/31" 6 "Late: 4/01-4/07 "  7 "No order") row(3) region(lcolor(none)) ) xti("")) ///
(line `v' date if eff_date==2, lcolor("187 47 41")) ///
(line `v' date if eff_date==3, lcolor("228 95 71")) ///
(line `v' date if eff_date==4,  lcolor("238  151 121"))  ///
(line `v' date if eff_date==5,  lcolor("243 190 165"))  

graph export "${figures}/`v'_caltime.pdf", replace
restore
}

/*
color red_5= "89 13 17"
color red_4= "187 47 41"
color red_3= "228 95 71"
color red_2= "238  151 121"
color red_1= "243 190 165"
*/

